package com.edu.bean;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;

import java.io.InputStream;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @Author:mx
 * @Date:2025/11/7-10:45:07
 */
public class MyIOC3 {

    private Map<Object,Object> box = new HashMap<>();//用于装对象

    private Document document;//代表读取到 的xml配置

    public MyIOC3(String path){
        readXml(path);
    }

    public void readXml(String path){
        SAXReader reader = new SAXReader();
        InputStream inputStream = MyIOC3.class.getClassLoader().getResourceAsStream(path);
        try {
            document =  reader.read(inputStream);
            System.out.println(document);
        } catch (DocumentException e) {
            System.out.println("文件不存在，或者无法读取");
            e.printStackTrace();
        }
        createObject();
    }

    public void createObject(){
        //找到所有的bean节点
        List<Node> beanNodes = document.selectNodes("//beans/bean");
        for (Node bean : beanNodes) {
            String beanId = bean.valueOf("@id");
            String className = bean.valueOf("@class");
            try {
                Class clazz = Class.forName(className);
                Object o = clazz.newInstance();
                box.put(beanId,o);
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        di();

    }

    public void di(){
        //找到所有的键 即beanId
        Set<Object> keySet = box.keySet();
        //找到每个beanId对应的 bean配置
        for (Object key : keySet) {
            Node bean = document.selectSingleNode("//beans/bean[@id='"+key.toString()+"']");
            setProperty(bean,box.get(key));
        }

    }

    public Object getBean(String beanId){
        return box.get(beanId);
    }

    public void setProperty(Node bean,Object o){
        List<Node> properties = bean.selectNodes("property");
        for (Node property : properties) {
            String filedName = property.valueOf("@name");
            String value = property.valueOf("@value");
            String ref = property.valueOf("@ref");
            Class clazz = o.getClass();
            try {
                if(value != null && !value.isEmpty()) {
                    Field field = clazz.getDeclaredField(filedName);
                    field.setAccessible(true);
                    Class type = field.getType();
                    field.set(o, typeTrans(value, type));
                }
                if(ref != null && !ref.isEmpty()){
                    Field field = clazz.getDeclaredField(filedName);
                    field.setAccessible(true);
                    Object refBean = box.get(ref);
                    field.set(o,refBean);
                }
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                System.out.println("--");
                e.printStackTrace();
            }
        }
    }

    //负责把 字符串类型 转换成 对象的属性 的原本类型
    public Object typeTrans(String str, Class targetClass){
        String typeName = targetClass.getSimpleName();
        switch(typeName){
            case "int": case "Integer": return Integer.parseInt(str);
            case "long": case "Long": return Long.parseLong(str);
            case "boolean": case "Boolean": return Boolean.parseBoolean(str);
            case "double": case "Double": return Double.parseDouble(str);
            case "float": case "Float": return Float.parseFloat(str);
            default: return str;
        }
    }


}
